package cn.ad.service;

import cn.ad.exception.CreativeGroupValidateException;
import cn.ad.handler.SqlSessionHandler;
import cn.ad.interceptor.PageParameter;
import cn.ad.model.CreativeGroupListModel;
import cn.ad.model.DropDownModel;
import cn.ad.entity.CreativeGroup;
import cn.ad.model.ResultSetList;
import com.alibaba.druid.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创意分组 服务接口
 * 
 * @author yujuan.zhang
 * @from 2016-08-29
 * @since DSP V2
 *
 */
@Service
public class CreativeGroupService {

	private static Logger logger = LoggerFactory.getLogger(CreativeGroupService.class);

	@Autowired
	private SqlSessionHandler sqlSessionHandler;

	/**
	 * 新增创意分组
	 */
	public CreativeGroup addCreativeGroup(CreativeGroup creativeGroup) {
		
		// 同一订单下创意分组名称不能重复
		if (hasCreativeGroup(creativeGroup)) {
			throw new CreativeGroupValidateException(CreativeGroupValidateException.ErrorCode.CREATIVE_GROUP_REPEAT);
		}
		
		sqlSessionHandler.insert("creativeGroupMapper.insert", creativeGroup);
		if (creativeGroup.getId() > 0) {
			logger.info("在订单{}下新增创意分组{}，创意分组名称为{}", creativeGroup.getCampaignId(), creativeGroup.getId(), creativeGroup.getName());
			return creativeGroup;
		} else {
			logger.error("在订单{}下新增创意分组失败", creativeGroup.getCampaignId());
			return null;
		}
	}

	// 同一订单下创意分组名称不能重复
	private boolean hasCreativeGroup(CreativeGroup creativeGroup) {
		return (int)sqlSessionHandler.selectOne("creativeGroupMapper.creativeGroup:checkname", creativeGroup) > 0;
	}
	
	/**
	 * 编辑创意分组
	 */
	public CreativeGroup editCreativeGroup(CreativeGroup creativeGroup) {
		// 根据订单id和创意分组id， 查询该订单下是否存在该创意分组
		CreativeGroup creativeGroupFromDB = sqlSessionHandler.selectOne("creativeGroupMapper.select:by:id:and:campaignId", creativeGroup);
		if (creativeGroupFromDB == null) {
			logger.error("在订单{}下不存在该创意分组{}", creativeGroup.getCampaignId(), creativeGroup.getId());
			throw new CreativeGroupValidateException(CreativeGroupValidateException.ErrorCode.CREATIVE_GROUP_NOTFOUND);
		}
		
		if (!StringUtils.equals(creativeGroup.getName(), creativeGroupFromDB.getName())) {
			// 同一订单下创意分组名称不能重复
			if (hasCreativeGroup(creativeGroup)) {
				throw new CreativeGroupValidateException(CreativeGroupValidateException.ErrorCode.CREATIVE_GROUP_REPEAT);
			}
		}
		
		// 更新创意分组信息
		sqlSessionHandler.update("creativeGroupMapper.update", creativeGroup);
		
		return selectCreativeGroupById(creativeGroup.getId());
	}

	/**
	 * 查询创意分组详细信息
	 */
	public CreativeGroup selectCreativeGroupById(Integer id) {
		return sqlSessionHandler.selectOne("creativeGroupMapper.selectById", id);
	}

	/**
	 * 更新创意分组的状态
	 */
	public void updateCreativeGroupStatus(Integer cid, Integer id, Integer status) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		params.put("campaignId", cid);
		params.put("status", status);
		sqlSessionHandler.update("creativeGroupMapper.update:status", params);
		logger.info("更改了创意分组状态ID{}状态值为{}",id,status);
	}

	/**
	 * 查询创意分组列表
	 */
	public ResultSetList<CreativeGroupListModel> findCreativeGroups(Map<String, Object> searchMap, PageParameter pageInfo) {
		List<CreativeGroupListModel> list = sqlSessionHandler.selectList("creativeGroupMapper.select:list:page", searchMap, pageInfo);
		
		// 封装成BO对象
		return new ResultSetList<>(pageInfo.getTotalCount(), list);
	}

	@Transactional
	public List<CreativeGroup> findCreativeGroupsByStaregyId(Integer staregyId) {
		return sqlSessionHandler.selectList("creativeGroupMapper.selectCreativeGroupsByStrategyId",staregyId);
	}

	public List<CreativeGroup> findCreativeGroupsByStaregyIdFromFm(Integer staregyId) {
		//selectCreativeGroupsByStrategyIdFromFM
		return sqlSessionHandler.selectList("creativeGroupMapper.selectCreativeGroupsByStrategyIdFromFM",staregyId);

	}

	/**
	 * 获取订单下的创意分组下拉列表
	 */
	public List<DropDownModel> findCreativeGroupList(Integer campaignId) {
		return sqlSessionHandler.selectList("creativeGroupMapper.select:dropdown:list", campaignId);
	}
	
}
